package com.jml.redis.事务;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

public class RedisTranaction {
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	public void setString(String key, Object object) {
		stringRedisTemplate.setEnableTransactionSupport(true);
		// 开启事务
		stringRedisTemplate.multi();
		try {
			// 如果是String 类型
			String value = (String) object;
			stringRedisTemplate.opsForValue().set(key, value);
		} catch (Exception e) {
			// 回滚
			stringRedisTemplate.discard();
		} finally {
			// 提交
			stringRedisTemplate.exec();
		}
	}


}
